package com.example.neteasecloudmusic.ui;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.WindowManager;

import com.example.neteasecloudmusic.R;
import com.example.neteasecloudmusic.ui.base.BaseActivity;
import com.example.neteasecloudmusic.ui.cloudhometown.CloudHomeTownFragment;
import com.example.neteasecloudmusic.ui.dicovery.DiscoveryFragment;
import com.example.neteasecloudmusic.ui.follow.FollowFragment;
import com.example.neteasecloudmusic.ui.mine.MineFragment;
import com.example.neteasecloudmusic.ui.song_list_detail.PlayerFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private BottomNavigationView bottomNavigationView;
    private Fragment[] fragments;
    private int lastFragmentIndex = 0;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Theme_NetEaseCloudMusic);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initViews() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        bottomNavigationView = findViewById(R.id.main_bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
        fragments = new Fragment[]{
                new DiscoveryFragment(),
                new PlayerFragment(),
                new MineFragment(),
                new FollowFragment(),
                new CloudHomeTownFragment()
        };
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.main_frame, fragments[0])
                .commit();
    }


    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        item.setChecked(true);
        switch (item.getItemId()) {
            case R.id.bottom_discovery:
                switchFragment(0);
                break;
            case R.id.bottom_player:
                switchFragment(1);
                break;
            case R.id.bottom_mine:
                switchFragment(2);
                break;
            case R.id.bottom_follow:
                switchFragment(3);
                break;
            case R.id.bottom_cloudhometown:
                switchFragment(4);
                break;
        }
        return false;
    }

    private void switchFragment (int to) {
        if (lastFragmentIndex == to) return;

        FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                .beginTransaction();
        if (!fragments[to].isAdded()) {
            fragmentTransaction.add(R.id.main_frame, fragments[to]);
        } else {
            fragmentTransaction.show(fragments[to]);
        }
        fragmentTransaction.hide(fragments[lastFragmentIndex]).commitAllowingStateLoss();
        lastFragmentIndex = to;

    }

}
































